package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {

        int pageSize = 30;
        if (rows != null && rows > 0) {
            pageSize = rows;
        }
        int offset = 0;
        if (page != null && page > 1) {
            offset = pageSize * (page - 1);
        }

        List<Supplier> goods = customerDao.listPage(offset, pageSize, customerName);

        HashMap<String, Object> map = new HashMap<>();

        map.put("total", customerDao.countInteger());
        map.put("rows", goods);

        return map;

    }

    @Override
    public void saveOrUpdate(Integer id, Customer customer) {
        if (id != null){
            //更新
            customer.setCustomerId(id);
            customerDao.update(customer);


        }else {
            //修改
            customer.setCustomerId(null);
            customerDao.save(customer);
        }


    }

    @Override
    public void delete(Integer[] ids) {
        customerDao.delete(ids);
    }
}
